﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FAS.App_Code;
using FAS.Objetos;
using System.Net;
using System.IO;

namespace FasGUI
{
    public partial class FormFiltroProblemas : Form
    {
        private Form a;
        public String objetivo;
        public int IdMateria;
        public FormFiltroProblemas(Form a,String objetivo,int IdMateria)
        {
            this.a = a;
            this.objetivo = objetivo;
            this.IdMateria = IdMateria;
            InitializeComponent();
            fillAll();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            new NuevoProblema(this).Show();
            this.Hide();
        }

        private DataGridViewTextBoxCell getCell(String value)
        {
            DataGridViewTextBoxCell celda = new DataGridViewTextBoxCell();
            celda.Value = value;
            return celda;
        }

     

        public void fillAll()
        {
            this.dataGridView1.Rows.Clear();
            ConexionMySQL conexion = new ConexionMySQL();
            DataSet data = conexion.GetQuery("SELECT * FROM Problema where IdMateria = '"+this.IdMateria+"' AND IdObjetivo = '"+this.objetivo+"'");
            this.dataGridView1.Rows.Clear();
            foreach (DataRow rowD in data.Tables[0].Rows)
            {
                string IdProblemaStr = rowD["IdProblema"].ToString();
                DataGridViewRow row = new DataGridViewRow();
                row.Cells.Add(getCell(IdProblemaStr));
                DataGridViewLinkCell l = new DataGridViewLinkCell();
                l.Value = "Ver";
                row.Cells.Add(l);
                DataGridButtonCellPayo button = new DataGridButtonCellPayo(int.Parse(IdProblemaStr));
                button.Value = "Eliminar";
                row.Cells.Add(button);
                this.dataGridView1.Rows.Add(row);
            }
           
            this.dataGridView1.Columns[1].AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            this.dataGridView1.Columns[1].AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
        }

        private void clickdgv(object sender, DataGridViewCellEventArgs e)
        {
            int row = dataGridView1.SelectedCells[0].RowIndex;
            if (e.ColumnIndex == 1) //CAMBIO
            {
                Problema problema = FAS.Objetos.Problema.GetProblema(int.Parse(this.dataGridView1.Rows[row].Cells[0].Value.ToString()));
                new DetallesProblema(problema, this).Show();
                this.Hide();
            }
            else if(e.ColumnIndex == 2) //CAMBIO
            {
                if (MessageBox.Show("Esta Seguro de Borrar el Problema?", "Confirmar", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    int p = ((DataGridButtonCellPayo)(this.dataGridView1.Rows[row].Cells[2])).Problema;
                    Problema problema = Problema.GetProblema(p);
                    String IdProblema = problema.Nombre.Split('/')[4].Split('.')[0];
                    //WebService.eliminaProblema(IdProblema);
                    problema.delete();
                    this.fillAll();
                    MessageBox.Show("Se Ha Eliminado el Problema");
                }
            }
        }

        private void FormFiltroProblemas_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
            a.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
            a.Show();
        }       
    }
}